<?php

namespace app\api\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Config;
// use think\facade\Session;
use app\common\model\SystemAdmin;
use app\common\model\User;
use app\common\model\Store;
use app\common\model\ProductType;
use app\common\model\ProductCollectible;
use app\common\model\ProductBargain;
use app\common\model\Product;
use app\common\model\ProductBanner;
use think\facade\Request;

class ApiUserProduct extends ApiBase
{
    public function initialize()
    {
        parent::initialize();
        $this->user = new User;
        $this->Store = new Store;
        $this->ProductType = new ProductType;
        $this->Product = new Product;
        $this->ProductBanner = new ProductBanner;
        $this->ProductCollectible = new ProductCollectible;
        $this->ProductBargain = new ProductBargain;
    }

     // 商品类型Api
     public function apiType()
     {

        $data = request()->post();

        if(empty($data['store_id'])){
            return json(['status' => 0, 'msg' =>"请上传店铺ID"]);
        }

        $store_id = $data['store_id'];
        
         if(empty($data['parent_id'])){
             $data['parent_id'] = 0;
         }
 
         $typeList = $this->ProductType->where(['store_id'=>$store_id,"deleted_at"=>null,"parent_id"=>$data['parent_id']])->order('sort',"asc")->field("id,name,sort,parent_id,store_id")->select()->toArray();
         foreach($typeList as &$v){
            $v['list'] =  $this->ProductType->where(['store_id'=>$store_id,"deleted_at"=>null,"parent_id"=>$v['id']])->order('sort',"asc")->field("id,name,sort,parent_id,store_id")->select()->toArray();
         }
 
         return json(['status' => 1, 'data' => $typeList]);
 
     }


     // 商品列表
     public function productList()
     {
         
        // 存储空间对应的域名
        $domain = config('sysconfig.qiniu_domain');


        $data = request()->post();

        if(empty($data['store_id'])){
            return json(['status' => 0, 'msg' =>"请上传店铺ID"]);
        }

        $store_id = $data['store_id'];


        $where[] = ['store_id',"=",$store_id];
        $where[] = ['deleted_at',"=",null];
        $where[] = ['status',"=",1];

        if(!empty($data['keyword'])){
            $where[] = ["name","like", "%" . $data['keyword'] . "%"];
        }

        if(!empty($data['type_id'])){
             $where[] =['type_id',"=",$data['type_id']];
        }

        if(!empty($data['type_child_id'])){
            $where[] =['type_child_id',"=",$data['type_child_id']];
        }

        if(!empty($data['min_money'] && !empty($data['max_money']))){
            $where[] = ['price',"Between",[$data['min_money'],$data['max_money']]];
        }

        
        if(empty($data['limit'])){
            $data['limit'] = 10;
        }

        if(empty($data['page'])){
            $data['page'] = 1;
        }

        $orderBy = "sort asc";
        if(!empty($data['order_type'])){
            switch($data['order_type']){
                case 1:
                    $orderBy = "sort asc";
                    break;
                case 2:
                    $orderBy = "sum_number desc";
                    break;
                case 3:
                    $orderBy = "id desc";
                    break;
            }
        }

        $list = $this->Product->getPagelist($where,"id,name,sort,logo,price,store_id,sum_number,type_id",$orderBy);

        foreach($list as $key=>$val){
            // $val->logo = $domain."/".$val['logo'];
            $val->type_name = $this->ProductType->where(['id'=>$val['type_id']])->value("name");
            
            
        }

        $count = $this->Product->countRows($where);

        $pages = ceil($count/$data['limit']);

        return json(['status'=> 1,'data'=>$list,'pages'=>$pages,"count"=>$count]);

     }


     // 商品详情
    public function getProject()
    {
        // 存储空间对应的域名
        $domain = config('sysconfig.qiniu_domain');

        // 判断店铺是否正常
        $data = request()->post();


        if(empty($data['store_id'])){
            return json(['status' => 0, 'msg' =>"请上传店铺ID"]);
        }
    

        if(empty($data['id'])){
            return json(['status' => 0, 'msg' =>"请上传产品ID"]);
        }

        $list = $this->Product->getOne(['id' =>$data['id'],'deleted_at'=>null]);

        if(empty($list)){
            return json(['status' => 0, 'msg' =>"商品不存在"]);
        }


        $list['content'] = str_replace("api_project",$domain."/api_project",$list['content']);

        if(empty($list)){
            return json(['status' => 0, 'msg' =>"请产品不存在"]);
        }

        // $list['logo'] = $domain."/".$list['logo'];

        $list['type_name'] = $this->ProductType->where(['id'=>$list['type_id']])->value("name");

        if(!empty($list['type_child_id'])){
            $list['type_child_name'] = $this->ProductType->where(['id'=>$list['type_child_id']])->value("name");
        }else{
            $list['type_child_name'] = "";
        }

        if($list['video_or_banner'] == 2){
            $list['banner_list'] = $this->ProductBanner->where(["product_id"=>$data['id']])->field("id,file_url")->select();

            if(!empty($list['banner_list'])){
                foreach($list['banner_list'] as $key=>$val){
                    $val->file_url = $domain."/".$val['file_url'];
                }
            }
        }

        $Collectible = $this->ProductCollectible->getOne(['user_id' => $this->uid,"store_id"=>$data['store_id'],"product_id"=>$data['id']]);

        if(!empty($Collectible)){
            $list['is_collectible'] = 1;
        }else{
            $list['is_collectible'] = 0;
        }

        $res =  $this->Product->where(['id' =>$data['id']])->inc('sum_number')->update();
        $res2 =  $this->Product->where(['id' =>$data['id']])->inc('real_number')->update();

        
        return json(['status'=> 1,'data'=>$list]);

    }


    // 商品收藏
    public function collectibleGood(){
         
         $data = request()->post();
 
         if(empty($data['store_id'])){
             return json(['status' => 0, 'msg' =>"请上传店铺ID"]);
         }

         if(empty($data['product_id'])){
            return json(['status' => 0, 'msg' =>"请上传产品id"]);
        }


        $list = $this->ProductCollectible->getOne(['user_id' => $this->uid,"store_id"=>$data['store_id'],"product_id"=>$data['product_id']]);

        if(!empty($list)){
            $res = $this->ProductCollectible->where(['id'=>$list['id']])->delete();
            $is_collectible = 0;
        }else{
            $add['store_id'] = $data['store_id'];
            $add['user_id'] = $this->uid;
            $add['product_id'] =$data['product_id'];
            $add['created_at'] =date("Y-m-d H:i:s");
            $add['update_at'] =date("Y-m-d H:i:s");
            $res = $this->ProductCollectible->create($add);
            $is_collectible = 1;
        }

        return json(['status' => 1, 'msg' => '操作成功',"is_collectible"=>$is_collectible]);

    }


    // 砍一刀
    public function bargain()
    {
        $data = request()->post();
 
        if(empty($data['store_id'])){
            return json(['status' => 0, 'msg' =>"请上传店铺ID"]);
        }

        if(empty($data['product_id'])){
           return json(['status' => 0, 'msg' =>"请上传产品id"]);
       }

       $store_id = $data['store_id'];
 
       $list = $this->Store->getOne(['id' =>$store_id]);

       if($list['bargain_set'] != 1){
            return json(['status' => 0, 'msg' =>"此产品不参加砍一刀活动"]);
       }

       $listProduct = $this->Product->getOne(['id'=>$data['product_id'],]);

       if(empty($listProduct)){
            return json(['status' => 0, 'msg' =>"产品不存在"]);
       }

       if(!empty($listProduct['type_id'])){
           $typeName = $this->ProductType->where(['id'=>$listProduct['type_id']])->value("name");
       }else{
            $typeName = "";
       }

       if(!empty($listProduct['type_child_id'])){
            $typeChildName = $this->ProductType->where(['id'=>$listProduct['type_child_id']])->value("name");
        }else{
            $typeChildName = "";
        }

       $listBargain = $this->ProductBargain->getOne(['user_id' => $this->uid,"store_id"=>$data['store_id'],"product_id"=>$data['id']]);


       if(!empty($listBargain)){
            $time = time() - 86400;
            if($listBargain['time'] > $time){
                return json(['status' => 0, 'msg' =>"操作成功"]);
            }
       }

       $add = [
            "product_id"=>$data['product_id'],
            "product_name"=>$listProduct['name'],
            "price"=>$listProduct['price'],
            "type_id"=>$listProduct['type_id'],
            "type_child_id"=>$listProduct['type_child_id'],
            "type_name"=>$typeName,
            "type_child_name"=>$typeChildName,
            "logo"=>$listProduct['logo'],
            "content"=>$listProduct['content'],
            "bargain_set"=>$listProduct['bargain_set'],
            "bargain_people"=>$listProduct['bargain_people'],
            "bargain_price"=>$listProduct['bargain_price'],
            "status"=>1,
            "store_id"=>$data['store_id'],
            "time"=>time(),
            "bargain_people_num"=>0,
            "bargain_price2"=>0.00,
            "user_id"=>$this->uid,
       ];

       $res = $this->ProductBargain->create($add);

        return json(['status'=> 1,'msg'=> '操作成功']);

    }

    

   
    
}
